
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
namespace Syrinx
{
    public static class AuditLog
    {
		/// <summary>
		/// 
		/// </summary>
		public static IApplication app = Application.currentApp;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="level"></param>
		/// <param name="eventId"></param>
		/// <param name="appId"></param>
		/// <param name="data1"></param>
		/// <param name="data2"></param>
		/// <param name="clientId"></param>
		/// <param name="subjectId"></param>
		/// <param name="userAccountId"></param>
		/// <param name="subSystem"></param>
		public static void audit(int level, string eventId, string appId, string data1, string data2, decimal clientId, decimal subjectId, decimal userAccountId, string subSystem)
        {
            try
            {
                IBizObj audit = app.bizObjMgr.create("AuditInfo");
                audit.put("eventId", eventId);
				audit.put("time", DateTime.Now.ToUniversalTime());
                audit.put("data1", data1);
                audit.put("data2", data2);
                audit.put("ApplicationId", appId);
                audit.put("ClientId", clientId);
                audit.put("AccountId", subjectId);
                audit.put("UserAccountId", userAccountId);
                audit.put("Level", level);
                audit.put("SubSystem", subSystem != null ? subSystem : app.resMgr.getMacro("SubSystem"));
                audit.put("SessionId", app.sessionId);
                app.Audit.write(audit);
            }
            catch (Exception e) { createSystemEventLog(e); }

        }
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="level"></param>
		/// <param name="appId"></param>
		/// <param name="message"></param>
		/// <param name="method"></param>
		/// <param name="ruleName"></param>
		/// <param name="parameters"></param>
		/// <param name="result"></param>
		/// <param name="serverName"></param>
		/// <param name="subSystem"></param>
		/// <param name="clientId"></param>
		/// <param name="userAccountId"></param>
		/// <param name="subjectId"></param>
        public static void log(int level, string appId, string message, string method, string ruleName, string parameters, string result, string serverName, string subSystem, decimal clientId, decimal userAccountId, decimal subjectId)
        {
            try
            {
                IBizObj log = app.bizObjMgr.create("LogInfo");
                log.put("Level", level);
                log.put("Message", message);
                log.put("Method", method != null ? method : "METHODNOTFOUND!");
                log.put("ApplicationId", appId);
                log.put("ApplicationPageId", null);
                log.put("SessionId", app.sessionId);
                log.put("ClientId", clientId);
                log.put("AccountId", subjectId);
                log.put("UserAccountId", userAccountId);
                log.put("RuleName", ruleName != null ? ruleName : null);
                log.put("RuleParameters", parameters != null ? parameters : null);
                log.put("RuleResults", result != null ? result : null);
                log.put("ServerName", serverName != null ? serverName : "SYRINXDEVSERVER");
                log.put("SubSystem", subSystem != null ? subSystem : "SAXWEB");
                app.Log.println(level, log);
            }
            catch (Exception e) { createSystemEventLog(e); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
		public static void createSystemEventLog(Exception ex)
        {
            createSystemEventLog(ex,"NoMethodSpecified");
        }
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="ex"></param>
		/// <param name="methodName"></param>
		public static void createSystemEventLog(Exception ex, string methodName)
        {
			string errorMsg = processErrorMessage(ex);
			System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog("SAX");
			eventLog.Source = "SAX";
			eventLog.WriteEntry(errorMsg, System.Diagnostics.EventLogEntryType.Error);
			log((int)Swaf.Logging.LoggingLevels.High, "SAX", errorMsg, methodName,
                    null, null, null, null, null, 0, 0, 0);
        }
        
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="ex"></param>
		/// <returns></returns>
		public static string processErrorMessage(Exception ex)
		{
			StringBuilder buff = new StringBuilder();
			int deep = 0;
			if(ex!=null)
				while (ex != null)
				{
					buff.Append(
							(++deep == 1) ?
						"Critical Exception Details: " :
							(deep == 2) ?
						"Inner Exception Hierarchy: " :
						"Next InnerException:");
					buff.Append("\n");
					buff.Append(ex.ToString());
					buff.Append("\n");
					ex = ex.InnerException;
				}
			return buff.ToString();
		}

		static object calcId(decimal value)
		{
			if (value <= 0)
				return null;
			return value;
		}

    }
}
